package com.adcc.ags.gms.gw.service;

import com.adcc.ags.gms.core.entity.GWLogLevel;
import com.adcc.ags.gms.core.util.AppConfiguration;
import com.adcc.ags.gms.core.util.Constant;
import com.adcc.ags.gms.core.util.LogFactory;
import com.adcc.ags.gms.gw.entity.SysConfiguration;
import com.adcc.ags.gms.gw.mapper.SysConfigurationMapper;
import com.adcc.ags.gms.gw.vo.SysConfigurationVO;
import com.adcc.utility.codec.MD5;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service("SysConfigurationService")
public class SysConfigurationServiceImpl implements SysConfigurationService {

    @Autowired
    private SysConfigurationMapper sysConfigurationMapper;

    @Autowired
    private AppConfiguration configuration;

    @Autowired
    private LogFactory log;

    @Override
    public Optional<Map<String,List<Map<String, String>>>> getSysConfigs() {
        Map<String,List<Map<String, String>>> mapResult = Maps.newHashMapWithExpectedSize(1000);
        Map<String, String> map = Maps.newHashMapWithExpectedSize(1000);
        List<Map<String, String>> listMap = Lists.newArrayListWithExpectedSize(1000);
        try {
            // 查找数据
            List<SysConfiguration> list = sysConfigurationMapper.findAll();
            if (list.size() > 0) {
                for (SysConfiguration sysConfiguration : list) {
                    map.put(sysConfiguration.getKey(),sysConfiguration.getValue());
                }
            }
            else{
                map.put("qmConnNumber",configuration.getQmConnNumber());
                map.put("queueDepthThreshold",configuration.getQueueDepthThreshold());
            }
            listMap.add(map);
            mapResult.put("Result", listMap);
            return Optional.of(mapResult);
        } catch (Exception ex) {
            log.error(SysConfigurationService.class, "SysConfiguration", GWLogLevel.ALARM, "getSysConfigs() error", ex);
            map.put("qmConnNumber",configuration.getQmConnNumber());
            map.put("queueDepthThreshold",configuration.getQueueDepthThreshold());
            listMap.add(map);
            mapResult.put("Result", listMap);
            return Optional.of(mapResult);
        }
    }

    @Override
    public String getValue(String key) {
        String result = "";
        try {
            result = sysConfigurationMapper.getValue(key);
        } catch (Exception ex) {
            log.error(SysConfigurationService.class, "SysConfiguration", GWLogLevel.ALARM, "getValue() error", ex);
        }
        return result;
    }

    @Override
    public Optional<Boolean> addSysConfig(SysConfigurationVO vo) {
        try {
            SysConfiguration sysConfiguration = vo.toEntity();
            sysConfigurationMapper.add(sysConfiguration);
            return Optional.of(true);
        } catch (ParseException ex) {
            log.error(SysConfigurationService.class, "SysConfiguration", GWLogLevel.ALARM, "createConfiguration() error", ex);
            return Optional.absent();
        }
    }

    @Override
    public Optional<Boolean> updateSysConfig(String strArray) {
        try {
            if(!Strings.isNullOrEmpty(strArray)){
                Map<String,String> map = new ObjectMapper().readValue(strArray, Map.class);
                Iterator<Map.Entry<String, String>> entries = map.entrySet().iterator();
                while (entries.hasNext()) {
                    Map.Entry<String, String> entry = entries.next();
                    String key = entry.getKey();
                    String value = entry.getValue();

                    // 管理员和值班员密码加密存储
                    if (key.equals(Constant.ADMIN_USER_PWD) || key.equals(Constant.MONITOR_USER_PWD)) {
                        String md5Value = MD5.getMD5(value);
                        sysConfigurationMapper.update(key, md5Value);
                    } else {
                        sysConfigurationMapper.update(key, value);
                    }
                }
            }
            return Optional.of(true);
        } catch (Exception ex) {
            log.error(SysConfigurationService.class, "SysConfiguration", GWLogLevel.ALARM, "updateSysConfig() error", ex);
            return Optional.absent();
        }
    }
}
